package org.example.service.impl;

import com.alibaba.excel.EasyExcel;
import org.example.handler.excel.DeptSheetWriteHandler;
import org.example.handler.excel.EmpSheetWriteHandler;
import org.example.listener.excel.EmpSheetDataListener;
import org.example.pojo.bo.ExcelInfoBO;
import org.example.pojo.vo.DepartmentInfoVO;
import org.example.pojo.vo.EmployeeVO;
import org.example.service.CompanyInfoService;
import org.example.util.ExcelUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@Service
public class CompanyInfoServiceImpl implements CompanyInfoService {


    @Override
    public void downloadExcel(HttpServletResponse response) throws IOException {
        // 定义下拉选项内容
        Map<Integer, String[]> empDropDownMap = new HashMap<>();
        empDropDownMap.put(1, new String[]{"男", "女"}); // 性别列（索引1）的下拉选项
        empDropDownMap.put(2, new String[]{"人事部", "财务部", "技术部", "市场部", "运营部"}); // 部门列（索引2）的下拉选项

        // 准备示例数据
        List<EmployeeVO> employeeVOS = Arrays.asList(
                new EmployeeVO("张三", "男", "技术部", 28, "zhangsan@example.com"),
                new EmployeeVO("李四", "女", "人事部", 32, "lisi@example.com"),
                new EmployeeVO("王五", "男", "财务部", 45, "wangwu@example.com"),
                new EmployeeVO("赵六", "女", "市场部", 26, "zhaoliu@example.com"),
                new EmployeeVO("钱七", "男", "技术部", 30, "qianqi@example.com")
        );

        // 定义下拉选项内容
        Map<Integer, String[]> deptDropDownMap = new HashMap<>();
        deptDropDownMap.put(0, new String[]{"D001", "D002", "D003", "D004", "D005"});
        deptDropDownMap.put(1, new String[]{"技术部", "人事部", "财务部", "市场部", "运营部"});


        // 准备第二个 Sheet (部门信息) 的数据
        List<DepartmentInfoVO> departments = Arrays.asList(
                new DepartmentInfoVO("D001", "技术部", "技术总监", 50),
                new DepartmentInfoVO("D002", "人事部", "人事经理", 15),
                new DepartmentInfoVO("D003", "财务部", "财务总监", 20),
                new DepartmentInfoVO("D004", "市场部", "市场总监", 25),
                new DepartmentInfoVO("D005", "运营部", "运营经理", 30)
        );


        List<ExcelInfoBO.SheetInfo> sheetInfos = new ArrayList<>();

        ExcelInfoBO.SheetInfo empList = ExcelInfoBO.SheetInfo.builder()
                .sheetDataClass(EmployeeVO.class)
                .sheetData(employeeVOS)
                .sheetName("员工列表")
                .sheetWriteHandler(new EmpSheetWriteHandler(empDropDownMap))
                .build();
        sheetInfos.add(empList);

        ExcelInfoBO.SheetInfo deptList = ExcelInfoBO.SheetInfo.builder()
                .sheetDataClass(DepartmentInfoVO.class)
                .sheetData(departments)
                .sheetName("部门列表")
                .sheetWriteHandler(new DeptSheetWriteHandler(deptDropDownMap))
                .build();
        sheetInfos.add(deptList);

        ExcelInfoBO excelInfoBO = new ExcelInfoBO();
        excelInfoBO.setFileName("公司信息");
        excelInfoBO.setSheetList(sheetInfos);

        ExcelUtil.downloadExcel(response, excelInfoBO);

    }

    @Override
    public void uploadExcel(MultipartFile file) throws IOException {
        EmpSheetDataListener empSheetDataListener = new EmpSheetDataListener();
        // 读取文件流，绑定实体类和监听器
        EasyExcel.read(file.getInputStream(), EmployeeVO.class, empSheetDataListener)
                .sheet("员工列表") // 读取默认的第一个sheet，也可以指定 .sheet(0) 或 .sheet("Sheet1")
                .doRead();
    }
}
